//
//  AYMainTabBar.swift
//  iOSSinaWeb
//
//  Created by yws on 16/5/10.
//  Copyright © 2016年 yws. All rights reserved.
//

import UIKit

class AYMainTabBar: UITabBar {

    var composeCallback: (() ->())?
    
    
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //计算出每个item的宽度
        let width = frame.width / 5
        
        var index = 0
        
        for view in subviews{
            if view.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                view.frame = CGRect(x: CGFloat(index) * width, y: 0, width: width, height: frame.height)
                index += 1
                
                if index == 2 {
                    index += 1
                }
                
            }
            
        }
        
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: self.frame.height)
    }
    
    // MARK: - btnClick
    func composeButtonClick(button: UIButton) {
        composeCallback?()
    }
    
    // MARK: - lazy
    //设置按钮的懒加载
    lazy var composeButton: UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        self.addSubview(btn)
        
        btn.addTarget(self, action: #selector(AYMainTabBar.composeButtonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    
}

